package com.ning.ems.controller;

import com.ning.ems.dto.PSIAddDto;
import com.ning.ems.dto.PSIListDto;
import com.ning.ems.entity.PackageStateInfo;
import com.ning.ems.service.PackageStateInfoService;
import com.ning.ems.vo.PSIVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Ning 242741154@qq.com
 * @date 2020/4/12 1:52
 */
@Api(tags = "包裹状态信息(PSI)管理相关接口")
@RestController
@RequestMapping("/psi")
public class PackageStateInfoController {
    private final PackageStateInfoService service;

    public PackageStateInfoController(PackageStateInfoService service) {
        this.service = service;
    }

    @ApiOperation("包裹对应的 PSI 列表")
    @GetMapping("/list1")
    private List<PSIVo> list(@RequestParam(value = "pkg") Integer pkg) {
        return service.list(pkg);
    }

    @ApiOperation("快递员经手的 PSI 列表")
    @PostMapping("/list2")
    private List<PackageStateInfo> list(@RequestBody PSIListDto info) {
        return service.list(info);
    }

    @ApiOperation("通过 id 获取 PSI")
    @GetMapping("/get")
    private PackageStateInfo get(@RequestParam(value = "id") Integer id) {
        return service.get(id);
    }

    @ApiOperation("添加 PSI")
    @PostMapping("/add")
    private PackageStateInfo add(@RequestBody @Valid PSIAddDto info) {
        PackageStateInfo psi = new PackageStateInfo();
        psi.setPkg(info.getPkg());
        psi.setCourier(info.getCourier());
        psi.setDate(LocalDateTime.now());
        return service.add(psi);
    }

    @ApiOperation("通过 id 删除 PSI")
    @DeleteMapping("/delete1")
    private boolean delete1(@RequestParam(value = "id") Integer id) {
        return service.deleteById(id);
    }

    @ApiOperation("通过 pkg 删除 PSI")
    @DeleteMapping("/delete2")
    private boolean delete2(@RequestParam(value = "id") Integer pkg) {
        return service.deleteByPkg(pkg);
    }
}
